//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    longitude: "",
    latitude: "",
    markers: [],
    controls: [{
        iconPath: '/resources/location.png',
        position: {
          left: (app.globalData.windowWidth / 2) - 14,
          top: ((app.globalData.windowHeight - 40) / 2) - 8,
          width: 30,
          height: 30
        }
      },
      {
        id: 1,
        iconPath: '/resources/loc.png',
        position: {
          left: 10,
          top: app.globalData.windowHeight - 90,
          width: 40,
          height: 40
        },
        clickable: true
      }
    ]
  },
  onShow() {
    // console.log("page");
    this.getLocation();
    this.getMessages();
  },
  getMessages() {
    wx.request({
      url: 'https://nuanwan.wekeji.cn/student/index.php/trade/get_list', //仅为示例，并非真实的接口地址
      data: {
        distinct: app.globalData.distinct
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: this.getMessagesSucc.bind(this)

    })
  },
  getMessagesSucc(res) {
    const data = res.data.data;
    const markers = data.map((value, index) => {
      return {
        iconPath: "/resources/"+value.type+".png",
        id: value.id,
        latitude: value.latitude,
        longitude: value.longitude,
        width: 40,
        height: 40
      }
    });
    this.setData({
      markers: markers
    })
    console.log(res.data)
  },
  onReady(e) {
    // 使用 wx.createMapContext 获取 map 上下文
    this.mapCtx = wx.createMapContext('map')
  },
  getLocation() {
    wx.getLocation({
      type: 'gcj02',
      success: this.handleGetLocationSucc.bind(this)
    })
  },
  handleGetLocationSucc(res) {
    //console.log(res);
    this.setData({
      longitude: res.longitude,
      latitude: res.latitude
    })
  },
  controltap(e) {
    this.mapCtx.moveToLoaction();
    //console.log(e);
    // const mpCtx=wx.createMapContext("map");
    // mpCtx.moveToLoaction();
  },
  onShareAppMessage() {
    return {
      title: "萌宠交易平台",
      path: "/pages/index/index"
    }
  },
  handleMarkertap(e){
    //console.log(e);
    //微信小程序用?带参数
    wx.navigateTo({
      url: '/pages/detail/detail?id='+e.markerId,
    })

  }

})